package com.fdb.efp.console.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.efp.console.api.service.RiskmUtilService;
import com.fdb.efp.console.service.facade.RscTaskLoanService;
import com.fdb.efp.console.service.vo.RscTaskLoanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>Description:风险分类台账				<p>
 * @author luzy
 * @date 2019-09-07
 */
@RestController
@RequestMapping("/api")
public class RscTaskLoanController extends AbstractController {

	@Autowired
	@Qualifier("rscTaskLoanService")
	private RscTaskLoanService rscTaskLoanService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	private RiskmUtilService riskmUtilService;
	/**
	 * 列表数据查询
	 * @param
	 * @param
	 * @return
	 */
	@RequestMapping(value="/rsc/task/loans", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<RscTaskLoanVO>> queryRscTaskLoanAll(@RequestBody RscTaskLoanVO vo) {
		List<RscTaskLoanVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String, String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
			logger.info("开始导出数据！");
			switch (dataRuleType) {
				case Owner:
					outputVo = rscTaskLoanService.queryExAllOwner(vo);
					break;
				case CurrOrg:
					outputVo = rscTaskLoanService.queryExAllCurrOrg(vo);
					break;
				case CurrDownOrg:
					outputVo = rscTaskLoanService.queryExAllCurrDownOrg(vo);
					break;
				case CurrOwnerPrd:
					outputVo = rscTaskLoanService.queryExAllCurrOwnerPrd(vo);
					break;
				default:
					outputVo = rscTaskLoanService.queryExAllOwner(vo);
					break;
			}
		} else {
			logger.info("开始查询数据！");
			// 根据权限方案调用不同的方案进行数据查询
			switch (dataRuleType) {
				case Owner:
					outputVo = rscTaskLoanService.queryAllOwner(vo);
					break;
				case CurrOrg:
					outputVo = rscTaskLoanService.queryAllCurrOrg(vo);
					break;
				case CurrDownOrg:
					outputVo = rscTaskLoanService.queryAllCurrDownOrg(vo);
					break;
				case CurrOwnerPrd:
					outputVo = rscTaskLoanService.queryAllCurrOwnerPrd(vo);
					break;
				default:
					outputVo = rscTaskLoanService.queryAllOwner(vo);
					break;
			}
		}
		riskmUtilService.displayOrgName(outputVo,"mainBrId","mainBrName");
		riskmUtilService.displayOrgName(outputVo,"inputBrId","inputBrName");
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/rsc/task/loan/{taskNo}{billNo}/", method=RequestMethod.GET)
	public @ResponseBody ResponseData<RscTaskLoanVO> queryByPk(@PathVariable("billNo") String billNo) {
		RscTaskLoanVO inVo = new RscTaskLoanVO();
		inVo.setBillNo(billNo);
		RscTaskLoanVO outputVo = rscTaskLoanService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * @param inRscTaskLoanVo
	 * @return
	 */
	@RequestMapping(value="/rsc/task/loan", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody RscTaskLoanVO inRscTaskLoanVo) {
		int outputVo = rscTaskLoanService.deleteByPk(inRscTaskLoanVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * @param inRscTaskLoanVo
	 * @return
	 */
	@RequestMapping(value="/rsc/task/loan", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody RscTaskLoanVO inRscTaskLoanVo) {
		int outputVo = rscTaskLoanService.updateByPk(inRscTaskLoanVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * @param inRscTaskLoanVo
	 * @return
	 */
	@RequestMapping(value="/rsc/task/loan", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertRscTaskLoan(@RequestBody RscTaskLoanVO inRscTaskLoanVo) {
		int outputVo = rscTaskLoanService.insertRscTaskLoan(inRscTaskLoanVo);
		return getResponseData(outputVo);
	}
	/**
	 * 导出数据
	 *
	 *
	 */
	@RequestMapping(value = "/rsc/task/sign/allInfos", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<RscTaskLoanVO>> queryCfcaSignInfoByCondition(@RequestBody RscTaskLoanVO cfcaSignInfoVo) {
		List<RscTaskLoanVO> outputVo = Optional.ofNullable(rscTaskLoanService.querySignAll(cfcaSignInfoVo)).orElseGet(ArrayList::new);
		return getResponseData(outputVo);
	}

	@RequestMapping(value="/rsc/task/loans/adjust", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<RscTaskLoanVO>> queryRscTaskLoanAllAdjust(@RequestBody RscTaskLoanVO vo) {
		List<RscTaskLoanVO> outputVo = null;
		outputVo = rscTaskLoanService.queryAllAdjustByPage(vo);
		riskmUtilService.displayOrgName(outputVo,"mainBrId","mainBrName");
		riskmUtilService.displayOrgName(outputVo,"inputBrId","inputBrName");
		return getResponseData(outputVo);
	}
}
